<template>
  <a-modal
    title="配置门店"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="门店名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            v-decorator="['store_name', {rules: [{required: true, min: 3, message: '请输入至少3个字符'}]}]"
          />
        </a-form-item>
        <a-form-item label="商家用户名" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="商家后台登录用户名">
          <a-input
            disabled
            v-decorator="['user_name', {rules: [{required: true, min: 4, message: '请输入至少4个字符'}]}]"
          />
        </a-form-item>
        <a-form-item label="分店数量" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="允许铺设分店数量">
          <a-input-number
            :min="1"
            v-decorator="['branch_number', {initialValue: 1, rules: [{required: true, message: '请输入铺设分店数量'}]}]"
          />
        </a-form-item>
        <a-form-item label="用户密码" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="登录密码为空表示不修改">
          <a-input
            type="password"
            v-decorator="['password', {rules: [
              {min: 6, message: '请输入至少6个字符'}
            ]}]"
          />
        </a-form-item>
        <a-form-item label="确认密码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            type="password"
            v-decorator="['password_confirm', {rules: [
              {message: '请输入确认密码'},
              {validator: compareToFirstPassword}
            ]}]"
          />
        </a-form-item>

        <a-form-item label="单次发放额度" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="抚恤金单次发放额度">
          <a-input-number
            :min="0"
            v-decorator="['quota', {initialValue: 5000, rules: [{required: true, message: '请输入抚恤金单次发放额度'}]}]"
          />
        </a-form-item>
        <a-form-item label="发放阈值" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="抚恤金发放阈值">
          <a-input-number
            :min="0"
            v-decorator="['threshold', {initialValue: 1000, rules: [{required: true, message: '请输入抚恤金发放阈值'}]}]"
          />
        </a-form-item>
        <a-form-item label="审核状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group
            v-decorator="['is_check', {initialValue: 2, rules: [{ required: true }]}]"
          >
            <a-radio :value="0">待审核</a-radio>
            <a-radio :value="1">驳回</a-radio>
            <a-radio :value="2">通过</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { update as updateApi } from '@/api/store'

export default {
  data () {
    return {
      // 标签布局属性
      labelCol: {
        span: 7
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      record: {}
    }
  },
  methods: {
    /**
     * 显示对话框
     */
    edit (record) {
      // 显示窗口
      this.visible = true
      // 当前分类记录
      this.record = record
      // 设置默认值
      this.setFieldsValue()
    },

    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { record, form: { setFieldsValue } } = this
      console.log(record)
      // 设置表单内容
      this.$nextTick(() => {
        // eslint-disable-next-line no-undef
        setFieldsValue(_.pick(record, ['store_name', 'user_name', 'branch_number', 'password', 'password_confirm', 'is_check', 'quota', 'threshold']))
      })
    },

    /**
     * 验证确认密码是否一致
     */
    compareToFirstPassword (rule, value, callback) {
      const { form } = this
      if (value && value !== form.getFieldValue('password')) {
        return new Error('您输入的确认密码不一致')
      }
      return true
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        if (!errors) {
          this.onFormSubmit(values)
        }
      })
    },

    /**
     * 关闭对话框
     */
    handleCancel () {
      this.visible = false
      // 重置表单内容
      this.form.resetFields()
    },

    /**
     * 提交到后端api
     */
    onFormSubmit (values) {
      this.confirmLoading = true
      updateApi({ store_id: this.record['store_id'], form: values })
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    }

  }
}
</script>
